<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>事件处理</title>
</head>
<style type="text/css">
  button{
    outline: none;
    border: none;
    width: 6rem;
    height: 2.5rem;
    font-size: 1rem;
    background-color: red;
    color: white;
  }
  .bians{
    outline: none;
    border: none;
    width: 6rem;
    height: 2.5rem;
    font-size: 1rem;
    background-color: #ddd;
    color: #fff;
  }

  .box{
    width: 200px;
    height: 200px;
    background-color: teal;
  }
  .box2{
    width: 100px;
    height: 100px;
    background-color: silver;
  }

</style>
<body>
  <button>领取优惠</button>
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
  </ul>

  <div class="box">
    <div class="box2"></div>
  </div>

  <a href="">百度<a href="">淘宝</a></a>
</body>
<script>
  var obtn = document.getElementsByTagName("button")[0];
  // obtn.onclick = function(){
  //   this.innerHTML = "正在加载...";
  //   this.className = "bians";

  //   var _this = this;
  //   setTimeout(() => {
  //     _this.innerHTML = "加载更多";
  //     _this.className = "";
  //   }, 2000);
  // }

  // obtn.onclick = function(){
  //   this.innerHTML = "已领取";
  //   this.classList.toggle("bians");
  //   this.onclick = null;
  // }
  obtn.addEventListener('click', function(){
    this.innerHTML = "已领取";
    this.className = "bians";

    this.removeEventListener('click', agruments.callee, false)
  }, false);

  var oli = document.getElementsByTagName("li"),
      len = oli.length,
      item;
  for(var i = 0; i < len; i++){
    (function(i){
      item = oli[i];
      item.addEventListener('click', function(){
        console.log(oli[i].innerHTML);
      }, false);
    })(i)
    
  }

  //绑定事件函数不需要加on
  function addEven(el, type, fn){
    if(el.addeventListener){
      el.addeventListener(type, fn, false);
    }else if(el.attachEvent){
      el.attachEvent('on'+type, fn, function(){
        fn.call(el);
      });
    }else{
      el["on" + type] = fn;
    }
  };

  // 阻止冒泡
  function cancelBubble(e){
    var e = e || window.event;
    if(e.stopPropagation){
      e.stopPropagation();
    }else{
      e.cancelBubble = true;
    }
  };


  var odiv = document.getElementsByTagName("div");
  for(var i in odiv){
    (function(i){
      odiv[i].onclick = function(e){
        cancelBubble(e);
        console.log(i);
      }
    })(i)
    
  }

  
  
</script>
</html>